﻿namespace iFinance.Modules.Outgoings.ViewModels
{
    using System.ComponentModel;
    using System.Linq;
    using System.Windows.Input;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.ViewModel;

    /// <summary>
    /// 支出明细ViewModel
    /// </summary>
    public class OutgoingsDetailViewModel : NotificationObject
    {
        #region Ctor

        public OutgoingsDetailViewModel()
        {
            AddDetailCommand = new DelegateCommand(AddDetail);
        }

        #endregion

        #region 属性

        /// <summary>
        /// 总金额
        /// </summary>
        public decimal TotalAmount
        {
            get { return this.List.Sum(detail => { return detail.UnitPrice * detail.Count; }); }
        }

        private OutgoingsDetailListViewModel list = new OutgoingsDetailListViewModel();

        /// <summary>
        /// 支出明细列表
        /// </summary>
        public OutgoingsDetailListViewModel List
        {
            get { return this.list; }
            set
            {
                if (this.list != value)
                {
                    this.list = value;
                    RaisePropertyChanged(() => this.List);
                }
            }
        }

        private OutgoingsDetailItemViewModel currentItem = new OutgoingsDetailItemViewModel { Count = 1m };

        /// <summary>
        /// 当前的支出明细项
        /// </summary>
        public OutgoingsDetailItemViewModel CurrentItem
        {
            get { return this.currentItem; }
            set
            {
                if (this.currentItem != value)
                {
                    this.currentItem = value;
                    RaisePropertyChanged(() => this.CurrentItem);
                }
            }
        }

        #endregion

        #region 命令

        #region AddDetailCommand

        private ICommand addDetailCommand = null;

        /// <summary>
        /// 添加明细项命令
        /// </summary>
        public ICommand AddDetailCommand
        {
            get { return this.addDetailCommand; }
            set
            {
                if (this.addDetailCommand != value)
                {
                    this.addDetailCommand = value;
                    RaisePropertyChanged(() => this.AddDetailCommand);
                }
            }
        }

        /// <summary>
        /// 添加明细
        /// </summary>
        private void AddDetail()
        {
            //TODO 添加明细并更新总价            
            var detailItem = new OutgoingsDetailItemViewModel();
            //TODO 下面的逻辑需要修改，选择消费品应该放到构造函数中
            detailItem.CurrentConsumable = this.CurrentItem.CurrentConsumable;
            detailItem.UnitPrice = this.CurrentItem.UnitPrice;
            detailItem.Count = this.CurrentItem.Count;
            AddDetailItem(detailItem);

            //TODO 清空数据
            this.CurrentItem.Count = 1m;
            this.CurrentItem.UnitPrice = 0m;
            this.CurrentItem.ConsumableName = string.Empty;
        }

        void CurrentItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("Count") || e.PropertyName.Equals("UnitPrice"))
            {
                RaisePropertyChanged(() => this.TotalAmount);
            }
        }

        #endregion

        #endregion

        #region 方法

        public void AddDetailItem(OutgoingsDetailItemViewModel detailItem)
        {
            //TODO 解除事件之间的耦合
            detailItem.PropertyChanged += new PropertyChangedEventHandler(CurrentItem_PropertyChanged);
            this.List.Add(detailItem);
            RaisePropertyChanged(() => this.TotalAmount);
        }

        #endregion
    }
}
